<?php
/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/5/12
 * Time: 9:38
 */

namespace App\Business\DataLog;


use App\Business\Model\logBaseModel;
use App\Extend\UserIDInfo;
use Illuminate\Support\Facades\Auth;

abstract class LogBase
{
    use UserIDInfo;

    private static $logBaseModel=null;
    private static $UserID=null;

    private $_isSave=false;

    protected $log;

    abstract protected function defaultInitialize();


    /**
     * 初始化
     *
     */
    private function Initialize(){
        $this->defaultInitialize();
        if(!$this->log) return;




        try{


            $token=request()->session()->token();
            $ip_remote=request()->getClientIp();
            $ip_real= request()->header("x-real-ip");
            $x_forwarded_for=request()->header("x-forwarded-for");
            $uri=request()->getUri();
            $userID=$this->GetUserID();

            /*if(!self::$logBaseModel)
            {

                self::$logBaseModel=new logBaseModel();


                self::$logBaseModel->IP_real=$ip_real;
                self::$logBaseModel->IP_remote=$ip_remote;
                self::$logBaseModel->X_forwarded_for=$x_forwarded_for;
                self::$logBaseModel->Token=$token;
                self::$logBaseModel->URL=$uri;

                $userID= $this->getUserID();
                self::$logBaseModel->UserID=$userID;
            }else{
                $userID=self::$logBaseModel->UserID;
            }*/

           /*

            $this->log->userID=self::$logBaseModel->UserID;
            $this->log->IP_Real=self::$logBaseModel->IP_real;
            $this->log->IP_Remote=self::$logBaseModel->IP_remote;
            $this->log->IP_forwarded_for=self::$logBaseModel->X_forwarded_for;
            $this->log->token=self::$logBaseModel->Token;
            $this->log->URL=self::$logBaseModel->URL;

           */

            //$this->log->userID=self::$UserID;

            $this->log->token=$token;
            $this->log->userID=$userID;
            $this->log->IP_Remote=$ip_remote;
            $this->log->IP_Real=$ip_real;
            $this->log->IP_forwarded_for=$x_forwarded_for;
            $this->log->URL=$uri;
            

        }catch (\Exception $ex)
        {
            //在数据库迁移时会触发异常
            //$this->log=null;

        }




    }

    /**
     * 开始初始化
     */
    public function InstializeStart()
    {
        $this->Initialize();
    }



    /**
     * 执行
     */
    public function Insert()
    {
        if($this->_isSave||!$this->log) return;

        $this->_isSave=true;
        try{
            $this->log->save();
            $this->log=null;
        }catch (\Exception $ex)
        {
            //在数据库迁移时可能会出错，略过出错的问题
        }

    }

}